<?php
namespace Admin\Widget;
use Think\Controller;

class MenuWidget extends Controller {
    protected $menuFilePath = '';   //菜单文件路径

    public function __construct() {
        $this->menuFilePath = __DIR__ . '/../Conf/';

        parent::__construct();
    }

    public function main(){
        $menuFileConf = C('MENU_FILE');
        $userType     = C('RUN_LOGIN_USER.type');
        $userType     = 5;
        $userType     = empty($userType) || !array_key_exists($userType, $menuFileConf) ? 5 : $userType;
        $menuFile     = $menuFileConf[$userType];

        $menu = include($this->menuFilePath . "{$menuFile}.php");
        $mMenu = D('Menu');
        $mMenu->setDao($menu);

        if(!$r = $mMenu->getByPid(0)) {
            return '';
        }

        $this->assign('records', $r);
        $this->display('MenuWidget:main');
    }

    public function sub() {
        $menuFileConf = C('MENU_FILE');
        $userType     = C('RUN_LOGIN_USER.type');
        $userType = 5;
        $userType     = empty($userType) || !array_key_exists($userType, $menuFileConf) ? 5 : $userType;
        $menuFile     = $menuFileConf[$userType];

        $menu  = include(__DIR__  . "/../Conf/{$menuFile}.php");
        $mMenu = D('Menu');
        $mMenu->setDao($menu);

        $mainMenu = $mMenu->getByPid(0);
        $html = '';

        foreach($mainMenu as $v) {
            if(!$_menu2 = $mMenu->getByPid($v['id'])) {
                continue;
            }

            foreach($_menu2 as $v2) {
                $html .= "<dl mid='{$v['id']}'>";
                $html .= "<dt>{$v2['name']}</dt>";

                if($_menu3 = $mMenu->getByPid($v2['id'])) {
                    $html .= '<dd>';

                    foreach($_menu3 as $v3) {
                        $html .= "<a href='{$v3['url']}'>{$v3['name']}</a>";
                    }

                    $html .= '</dd>';
                }

                $html .= '</dl>';
            }
        }

        return $html;
    }

}
